﻿using System.Collections.Generic;
using System.Windows.Forms;
using ReaXML;
using SCH_Tools.DbManager;
using SCH_Tools.Modules;
using System.Data;

namespace SCH_Tools.Forms
{
    public partial class frmGridData : Form
    {
        Server _setServerExecution;
        public frmGridData()
        {
            InitializeComponent();

            pnlGrid.Visible = false;
            pnlProgress.Visible = false;

            pnlGrid.Dock = DockStyle.Fill;
            pnlProgress.Dock = DockStyle.Fill;

            lblFile.Text = string.Empty;
            lbProgress.Text = string.Empty;
        }

        public void ExecuteSqlStatement(Server obj, string SqlStatement)
        {
            pnlGrid.Visible = true;
            DBManagment ServerAdmin = new DBManagment();
            _setServerExecution = obj;
            ServerAdmin.MyServer = _setServerExecution;

            foreach (Control ctrlObj in pnlGrid.Controls)
            {
                ctrlObj.Dispose();
            }

            System.Windows.Forms.TabControl tabControl = new System.Windows.Forms.TabControl();

            tabControl.Visible = true;
            tabControl.Parent = pnlGrid;
            tabControl.Dock = DockStyle.Fill;


            int TableId = 0;

            DataSet dt = ServerAdmin.FillData(SqlStatement);
            if (dt != null)
            {
                foreach (DataTable itemTable in dt.Tables)
                {
                    TableId++;
                    System.Windows.Forms.TabPage newTab = new System.Windows.Forms.TabPage();
                    newTab.Text = string.Format("Script {0} Rows[{1}]", TableId, itemTable.Rows.Count);
                    tabControl.Controls.Add(newTab);

                    DataGridView dgDataResult = new DataGridView();
                    dgDataResult.Visible = true;
                    newTab.Controls.Add(dgDataResult);

                    dgDataResult.AutoGenerateColumns = true;
                    dgDataResult.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
                    dgDataResult.AllowUserToAddRows = false;
                    dgDataResult.AllowUserToDeleteRows = false;
                    dgDataResult.ReadOnly = true;
                    dgDataResult.Dock = DockStyle.Fill;
                    dgDataResult.DataSource = (itemTable);
                    dgDataResult.Update();
                }
            }
            tabControl.Visible = true;
            tabControl.Parent = pnlGrid;
        }

        public void ExecuteSqlFiles(Server obj, List<string> files, string pathOut, ServerDriver Driver, string PathOutput)
        {
            pbProgress.Value = 0;
            pnlProgress.Visible = true;
            pbProgress.Maximum = files.Count;
            foreach (string file in files)
            {
                lblFile.Text = file;
                pbProgress.Value++;
                lbProgress.Text = string.Format("File {0} of {1}", pbProgress.Value, files.Count);
                ManagmentCmd CmdAdmin = new ManagmentCmd();
                CmdAdmin.ExecuteSqlCmd(file, obj, Driver, PathOutput);
            }
        }
    }
}
